<?php
namespace Service;

use PhalApi\Exception\InternalServerErrorException;

/**
 * OSS组件
 * - 支持阿里云/腾讯云/七牛云/本地OSS存储
 * @author dogstar 2020-11-11
 */
class OSS {

    /**
     * 上传文件
     * @param array $file 上传的文件FILE
     * @return array 上传后的结果
     */
    public function upload($file) {
        throw new InternalServerErrorException('OSS未实现');
    }

    public static function create() {
        $config = \PhalApi\DI()->config;
        $ossType = $config->get('app.oss.oss_type');

        $cfgPath = 'app.oss.' . $ossType;
        $cfg = $config->get($cfgPath);
        if ($cfg === NULL) {
            throw new InternalServerErrorException('未找到OSS配置' . $cfgPath);
        }

        $class = 'Service\\OSS\\' . ucfirst($ossType) . 'OSS';
        if (!class_exists($class)) {
            throw new InternalServerErrorException('OSS类不存在' . $class);
        }

        $obj = new $class($cfg);
        return $obj;
    } 
}
